package com.way.itemexpandable.eample;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.way.eample.R;
import com.way.itemexpandable.ActionSlideExpandableListView;

/**
 * This example shows a expandable listview with a more button per list item
 * which expands the expandable area.
 * 
 * In the expandable area there are two buttons A and B which can be click.
 * 
 * The events for these buttons are handled here in this Activity.
 * 
 * @author tjerk
 * @date 6/13/12 7:33 AM
 */
public class ExampleActivity extends Activity {
	private List<String> items = new LinkedList<String>();
	private ActionSlideExpandableListView list;
	private MyAdapter adapter;
	private Toast toast;

	private void showToast(CharSequence message) {
		if (null == toast) {
			toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}

		toast.show();
	}

	/** Hide the toast, if any. */
	private void hideToast() {
		if (null != toast) {
			toast.cancel();
		}
	}

	@Override
	public void onCreate(Bundle savedData) {

		super.onCreate(savedData);
		// set the content view for this activity, check the content view xml
		// file
		// to see how it refers to the ActionSlideExpandableListView view.
		this.setContentView(R.layout.single_expandable_list);
		// get a reference to the listview, needed in order
		// to call setItemActionListener on it
		list = (ActionSlideExpandableListView) this.findViewById(R.id.list);

		// fill the list with data
		// list.setAdapter(buildDummyData());
		File f = new File("/");
		File[] files = f.listFiles();
		if (f != null && f.listFiles() != null) {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				items.add(file.getName());
			}
		}
		adapter = new MyAdapter(items);
		list.setAdapter(adapter);
		// listen for events in the two buttons for every list item.
		// the 'position' var will tell which list item is clicked
		list.setItemActionListener(
				new ActionSlideExpandableListView.OnActionClickListener() {

					@Override
					public void onClick(View listView, View buttonview,
							int position) {

						/**
						 * Normally you would put a switch statement here, and
						 * depending on view.getId() you would perform a
						 * different action.
						 */
						String actionName = "";
						if (buttonview.getId() == R.id.buttonA) {
							actionName = "rename";
						} else if (buttonview.getId() == R.id.buttonB) {
							actionName = "share";
						} else {
							actionName = "delete";
						}
						/**
						 * For testing sake we just show a toast
						 */
						showToast("sub click: " + actionName + " in list item "
								+ position);
					}

					// note that we also add 1 or more ids to the
					// setItemActionListener
					// this is needed in order for the listview to discover the
					// buttons
				}, R.id.buttonA, R.id.buttonB, R.id.buttonC);
	}

	public class MyAdapter extends BaseAdapter {
		private List<String> values;
		private LayoutInflater inflater;

		public MyAdapter(List<String> values) {
			// TODO Auto-generated constructor stub
			this.values = values;
			inflater = LayoutInflater.from(ExampleActivity.this);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return values.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return values.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.expandable_list_item,
						null);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.text);
			tv.setText(values.get(position));
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showToast("item click position = " + position);
				}
			});
			return convertView;
		}

	}
}
